﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using public_dbOpt;

namespace prjEMR22_154
{
    public partial class PatientInfo_new : Form
    {
        dbOPT dataOpt = new dbOPT();

        public PatientInfo_new()
        {
            InitializeComponent();
        }

        private void new_check()
        {
            string sql = String.Format("select * from patientinfo where HspID ='{0}'", HspID.Text);
            DataSet ds = new DataSet();//创建数据集
            ds = dataOpt.GetTable(sql);

            if (ds != null && ds.Tables[0].Rows.Count != 0)
            {
                // 处理用户存在的情况  
                MessageBox.Show("住院号已存在。");
            }
            else
            {
                sql = String.Format("INSERT INTO patientinfo (HspID) VALUES ({0})", HspID.Text);
                dataOpt.OperateData(sql);
                foreach (Control ctrl in this.Controls)
                {
                    if (ctrl is TextBox)
                    {
                        TextBox textbox = (TextBox)ctrl;
                        string text_name = textbox.Name == "Pa_Name" ? "Name" : textbox.Name;
                        sql = String.Format("UPDATE patientinfo SET {0} = '{1}' where HspID = '{2}'", text_name, textbox.Text, HspID.Text);   //set后跟的属性不能加''
                        dataOpt.OperateData(sql);
                    }

                    if (ctrl is ComboBox)
                    {
                        ComboBox textbox = (ComboBox)ctrl;
                        sql = String.Format("UPDATE patientinfo SET {0} = '{1}' where HspID = '{2}'", textbox.Name, textbox.Text, HspID.Text);
                        dataOpt.OperateData(sql);
                    }
                }
                MessageBox.Show("保存成功");
            }
        }

        private void PatientInfo_new_Load(object sender, EventArgs e)
        {
            dataOpt.BindDropdownlist("departinfo", DepartName, 1);
            string sql = string.Format("select * from doctorinfo where DepartName = '{0}' ", DepartName.Text);
            dataOpt.BindDropdownlist_sql(sql, DoctorName, 1);
        }

        private void save_Click(object sender, EventArgs e)
        {
            if (Pa_Name.Text != "" && Gender.Text != "" && Nation.Text != "" && HspID.Text != "" && DoctorName.Text != ""
               && DepartName.Text != "" && InHspDiagnose.Text != "")
            {
                new_check();
            }
            else
            {
                MessageBox.Show("必填项有空项，请重新输入");
            }
        }

        private void cencel_Click(object sender, EventArgs e)
        {
            foreach (Control ctrl in this.Controls)
            {
                if (ctrl is TextBox)
                {
                    TextBox textbox = (TextBox)ctrl;
                    textbox.Text = "";
                }

                if (ctrl is ComboBox)
                {
                    ComboBox textbox = (ComboBox)ctrl;
                    textbox.Text = "";
                }
            }
        }

        private void DepartName_SelectedIndexChanged(object sender, EventArgs e)
        {
            DoctorName.Items.Clear();
            string sql = string.Format("select * from doctorinfo where DepartName = '{0}' ", DepartName.Text);
            dataOpt.BindDropdownlist_sql(sql, DoctorName, 1);
        }



    }
}
